package com.rms.hkpc.activity;


import android.graphics.Color;
import android.widget.TextView;
import android.widget.Toast;

import com.rms.hkpc.R;
import com.rms.hkpc.activity.base.BaseActivity;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.db.dto.WorksOrderDto;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.presenter.RecordsDetailPresenter;
import com.rms.hkpc.util.DateUtil;
import com.rms.hkpc.util.WindowUtil;
import com.rms.hkpc.view.IRecordsDetailView;

import java.util.Date;

public class RecordsDetailActivity extends BaseActivity<IRecordsDetailView, RecordsDetailPresenter> implements IRecordsDetailView {

    private TextView tvTimeAgo, tvFirst, tvTwo, tvThird, tvNum;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_records_detail;
    }

    @Override
    protected void initView() {
        WindowUtil.getInstance().setWindowDecorView(this);
        tvTimeAgo = fd(R.id.tv_timeago);
        tvFirst = fd(R.id.tv_first);
        tvTwo = fd(R.id.tv_two);
        tvThird = fd(R.id.tv_third);
        tvNum = fd(R.id.tv_num);
        String id = getIntent().getStringExtra(AllConstants.ID);
        mPresenter.getRecords(id);
    }

    @Override
    protected RecordsDetailPresenter createPresenter() {
        return new RecordsDetailPresenter();
    }

    @Override
    public void onGetDataSuccess(ApiResultData<WorksOrderDto> apiResult) {
        if (apiResult.getStatus() != 2000) {
            Toast.makeText(this, apiResult.getMessage(), Toast.LENGTH_LONG).show();
            return;
        }
        WorksOrderDto data = apiResult.getData();
        Date date = data.getwAt() == null ?
                data.getModifyAt() : data.getwAt();
        tvThird.setText(DateUtil.getInstance().DateTimeToStr(date,"yyyy-MM-dd HH:mm:ss"));
        tvTimeAgo.setText(DateUtil.getInstance().getTimeFormatText(date));
        tvFirst.setText(data.getProject() == null ?
                getResources().getString(R.string.parameters) : data.getProject().getName());
        tvTwo.setText(data.getName() == null ? "" : data.getName());
        tvNum.setText(String.valueOf(data.getVal()));
        if (data.getParam() != null) {
            String color;
            if (data.getResult() == 0) {
                color = data.getParam().getFailCol() == null ? "#FF0000" : data.getParam().getFailCol();
            } else {
                color = data.getParam().getPassCol() == null ? "#FF0000" : data.getParam().getPassCol();
            }
            tvTwo.setTextColor(Color.parseColor(color));
            tvNum.setTextColor(Color.parseColor(color));
        }
    }

    @Override
    public void onGetDataFails(Exception msg) {
        Toast.makeText(this, R.string.network_error, Toast.LENGTH_LONG).show();
    }
}